package cn.mcsys.viewer.core.service.timer;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.viewer.entity.AirETickets;
import cn.mcsys.myadmin.modules.viewer.entity.AirRideQrCode;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bobo
 * @create 2018-09-17 17:53
 * @desc ${DESCRIPTION}
 **/
public class CheckTimer {


    @Autowired
    private BackService backService;

    /**
     * 查询过期的二维码和电子票和票务,将状态设置为过期
     */

    public void checkQrAndETick() {
        System.out.println("进入查询过期的二维码和电子票和票务定时器--------------使用的线程是:" + Thread.currentThread().getName());
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String current = dateFormat.format(currentDate);
        int pageNo = 0;

        while (true) {
            List<AirETickets> eTickets = backService.selectAllETickets(pageNo);
            List<Long> expiredETickets = new ArrayList<>();
            if (eTickets == null || eTickets.size() == 0) {
                break;
            }
            for (AirETickets eTicket : eTickets) {
                String ticketDate = eTicket.getTicketDate();
                ticketDate = ticketDate.replaceAll("\\.", "");
                if (current.compareTo(ticketDate) > 0) {
                    expiredETickets.add(eTicket.getId());
                }
            }
             if (expiredETickets.size() > 0) {
                backService.UpdateExpiredETickets(expiredETickets);
            }
            pageNo += 1000;
        }
        pageNo = 0;
        while (true) {
            List<AirRideQrCode> qrCodes = backService.selectAllQrCode(pageNo);
            List<Long> expiredQrCodes = new ArrayList<>();
            if (qrCodes == null || qrCodes.size() == 0) {
                break;
            }
            for (AirRideQrCode qrCode : qrCodes) {
                Date rideDate = qrCode.getRideDate();
                String ridedate = dateFormat.format(rideDate);
                if (current.compareTo(ridedate) > 0) {
                    expiredQrCodes.add(qrCode.getId());
                }
            }
            if (expiredQrCodes.size() > 0) {
                backService.UpdateExpiredQrCodes(expiredQrCodes);
            }
            pageNo += 1000;
        }

//        需要两千两千的查不然一次查出24W数据量太大了
        pageNo += 0;
        while (true) {
            List<AirTickets> tickets = backService.selectAllTickets(pageNo);
            if (tickets == null || tickets.size() == 0) {
                break;
            }
            List<Long> expiredTickets = new ArrayList<>();
            for (AirTickets ticket : tickets) {
                String ticketDate = ticket.getTicketDate();
                ticketDate = ticketDate.replaceAll("\\.", "");
                if (current.compareTo(ticketDate) > 0) {
                    expiredTickets.add(ticket.getId());
                }
            }
            if (expiredTickets.size() > 0) {
                backService.UpdateExpiredTickets(expiredTickets);
            }
            pageNo += 1000;
        }

    }


}
